package parser

import (
	"errors"
	"fmt"
	"project/schedule/model"
	"project/schedule/util"
)

// 检查固定项目人员的时间是否与他们的假期有冲突
func CheckLockConflict(general *model.General, lock *map[string]map[string]string, project *model.Project) error {
	for staff, lockInfo := range *lock {
		staffVacation := general.Staff[staff].Vacation
		conflict := util.CrossOverMulti(lockInfo["startDate"], lockInfo["endDate"], staffVacation)
		if conflict {
			msg := fmt.Sprintf("%s在项目`%s`的开始日期%s, 结束日期%s与他的假期有冲突",
				staff, project.Name, lockInfo["startDate"], lockInfo["endDate"])
			return errors.New(msg)
		}
	}
	return nil
}

// 更新固定项目的分配信息
func UpdateLockAllocation(general *model.General, lock *map[string]map[string]string, project *model.Project) {
	allocation := &model.Allocation{}
	allocation.DepartmentProposedStartDate = general.Department[project.Department].ProposedStartDate
	allocation.ProjectDepartment = project.Department
	allocation.ProjectProposedDay = project.ProposedPerson
	allocation.ProjectProposedPerson = project.ProposedPerson
	allocation.ProjectProposedStartDate = project.ProposedStartDate
	allocation.ProjectAllocatedStartDate = project.ProposedStartDate
	allocation.ProjectAllocatedEndDate = util.WorkDayIndex2Value[util.WorkDayValue2Index[project.ProposedStartDate]+project.ProposedDay-1]
	allocation.ProjectAllocatedDetail = *lock
	general.Allocation[project.Name] = allocation
}

// 更新人员工作区间
func UpdateLockRegion(general *model.General, lock *map[string]map[string]string, project *model.Project) {
	for staff, lockInfo := range *lock {
		role := lockInfo["role"]
		if role == "主审" {
			general.Allocation[project.Name].GroupJudge = staff
		}
		if role == "组长" {
			general.Allocation[project.Name].GroupLeader = staff
		}
		if role == "组员" {
			general.Allocation[project.Name].GroupMember =
				append(general.Allocation[project.Name].GroupMember, staff)
		}
	}
	workRegion := make(map[string]string)
	groupJudge := general.Allocation[project.Name].GroupJudge
	groupLeader := general.Allocation[project.Name].GroupLeader
	groupMember := general.Allocation[project.Name].GroupMember
	for k, v := range (*lock)[groupJudge] {
		if k == "role" {
			continue
		}
		workRegion[k] = v
	}
	general.Staff[groupJudge].WorkRegion = append(general.Staff[groupJudge].WorkRegion, workRegion)
	if general.Staff[groupJudge] != general.Staff[groupLeader] {
		for k, v := range (*lock)[groupLeader] {
			if k == "role" {
				continue
			}
			workRegion[k] = v
		}
		general.Staff[groupLeader].WorkRegion = append(general.Staff[groupLeader].WorkRegion, workRegion)
	}
	for _, member := range groupMember {
		for k, v := range (*lock)[member] {
			if k == "role" {
				continue
			}
			workRegion[k] = v
		}
		general.Staff[member].WorkRegion = append(general.Staff[member].WorkRegion, workRegion)
	}
}

// 组员是否需要用外包补齐
func UpdateLockAllocationWb(general *model.General, project *model.Project) {
	groupLeaderJudgeSlice := make([]string, 0)
	groupLeaderJudgeSlice = append(groupLeaderJudgeSlice, general.Allocation[project.Name].GroupJudge)
	groupLeaderJudgeSlice = append(groupLeaderJudgeSlice, general.Allocation[project.Name].GroupLeader)
	lack := project.ProposedPerson - len(general.Allocation[project.Name].GroupMember) - len(util.Duplicate(groupLeaderJudgeSlice))
	for i := 0; i < lack; i++ {
		general.Allocation[project.Name].GroupMember = append(general.Allocation[project.Name].GroupMember, "wb")
	}
}

// 如果assignDate为空字符串但是lock不为空, 则根据lock来推断出assignDate
func UpdateLockProposedStartDate(general *model.General, lock *map[string]map[string]string, project *model.Project) {
	if project.ProposedStartDate == "" {
		groupJudge := general.Allocation[project.Name].GroupJudge
		groupJudgeStartDate := (*lock)[groupJudge]["startDate"]
		before, _ := util.AuditDay(project.ProposedDay, "主审", general.AuditRule)
		project.ProposedStartDate = util.AllDayIndex2Value[util.AllDayValue2Index[groupJudgeStartDate]+before]
		general.Allocation[project.Name].ProjectProposedStartDate = project.ProposedStartDate
		general.Allocation[project.Name].ProjectAllocatedStartDate = project.ProposedStartDate
		general.Allocation[project.Name].ProjectAllocatedEndDate = util.WorkDayIndex2Value[util.WorkDayValue2Index[project.ProposedStartDate]+project.ProposedDay-1]
	}
}
